﻿Public Class Query
    Inherits System.Collections.Generic.Dictionary(Of String, Object)

    Public Overrides Function ToString() As String
        Dim pairs As New List(Of String)

        For Each pair In Me
            Dim pairType = pair.Value.GetType

            If pairType.IsDefined(GetType(FlagsAttribute), False) Then
                pairs.Add(
                    String.Format(
                        "{0}={1}",
                        pair.Key,
                        pair.Value.ToString).Replace(" ", String.Empty))
            Else
                Select Case pair.Value.GetType
                    Case GetType(Integer())
                        pairs.Add(
                            String.Format(
                                "{0}={1}",
                                pair.Key,
                                String.Join(
                                    ",",
                                    CType(pair.Value, Integer()))))

                    Case Else
                        pairs.Add(
                            String.Format(
                                "{0}={1}",
                                pair.Key,
                                pair.Value.ToString))

                End Select
            End If
        Next

        Return String.Format("?{0}", String.Join("&", pairs))
    End Function

    Public Property Keywords As String
        Get
            If Me!keywords IsNot Nothing Then
                Return System.Uri.UnescapeDataString(Me!keywords)
            End If

            Return Nothing
        End Get
        Set(ByVal value As String)
            Me!keywords = System.Uri.EscapeUriString(value)
        End Set
    End Property
    <System.ComponentModel.DefaultValue(False)>
    Public Property KeywordExact As Boolean
        Get
            If Me.ContainsKey("keywordExact") Then
                Return If(Me!keywordExact = 1, True, False)
            End If

            Return False
        End Get
        Set(ByVal value As Boolean)
            Me!keywordExact = If(value, 1, 0)
        End Set
    End Property
    Public Property OrderColumn As OrderColumn?
        Get
            If Me.ContainsKey("orderCol") Then
                Return Me!orderCol
            End If
        End Get
        Set(ByVal value As OrderColumn?)
            If value IsNot Nothing Then
                Me!orderCol = value
            Else
                Me.Remove("orderCol")
            End If
        End Set
    End Property
    Public Property SortBy As SortOrder?
        Get
            If Me.ContainsKey("sortBy") Then
                Return Me!sortBy
            End If
        End Get
        Set(ByVal value As SortOrder?)
            If value IsNot Nothing Then
                Me!sortBy = value
            Else
                Me.Remove("sortBy")
            End If
        End Set
    End Property
    <System.ComponentModel.DefaultValue(20)>
    Public Property ResultNumber As Integer
        Get
            If Me.ContainsKey("numResults") Then
                Return Me!numResults
            End If

            Return 20
        End Get
        Set(ByVal value As Integer)
            Me!numResults = value
        End Set
    End Property
    <System.ComponentModel.DefaultValue(0)>
    Public Property ResultOffset As Integer
        Get
            If Me.ContainsKey("resultOffset") Then
                Return Me!resultOffset
            End If

            Return 0
        End Get
        Set(ByVal value As Integer)
            Me!resultOffset = value
        End Set
    End Property
End Class